package com.liuhm.service.impl;


import com.liuhm.core.AbstractBroker;
import com.liuhm.core.CsvBroker;
import com.liuhm.core.thread.TrackThread;
import com.liuhm.entity.TableField;
import com.liuhm.service.FileHandleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * 数据文件处理
 * @author liuhaomin
 * @date 2020/10/30
 */
@Service
@Slf4j
//@Transactional
public class FileHandleServiceImpl implements FileHandleService {

	/**
	 * 创建 读线程的线程池
	 */
	@Value("${csv.read.thread-num}")
	private Integer csvReadThreadNum;

	@Override
	public void	importData(File file, int startRow, int columnSize, List<TableField>  tableFieldList) {

		AbstractBroker abstractBroker = new CsvBroker();

		/**
		 * 读数据
		 */
		new TrackThread(() -> {
			try{
				abstractBroker.producer(file,  startRow, columnSize);
			} catch (Exception e) {
				log.error("[读数据线程]异常,{}", e.getMessage());
			}
		}).start();


		/**
		 * 写数据
		 */
		new TrackThread(() -> {
			try{
				abstractBroker.setCsvReadThreadNum(csvReadThreadNum);
				abstractBroker.consumer(file,tableFieldList);
			} catch (Exception e) {
				log.error("[写数据线程]异常,{}", e.getMessage());
				// 清除导入状态
			}
		}).start();

	}

}
